<?php
$install = true;
require_once '../includes/common.php';

@header('Content-Type: text/html; charset=UTF-8');
if ($conf['web_version'] < 1001) {
    $dbconfig = require SYSTEM_ROOT . 'config.php';
    if (empty($dbconfig['dbqz'])) {
        $config = '<?php
/*数据库配置
return [
"host" => "' . $dbconfig['host'] . '", //数据库服务器
"port" => ' . $dbconfig['port'] . ', //数据库端口
"user" => "' . $dbconfig['user'] . '", //数据库用户名
"pwd" => "' . $dbconfig['pwd'] . '", //数据库密码
"dbname" => "' . $dbconfig['dbname'] . '", //数据库名
"dbqz" => "ucenter" //数据表前缀
];
?>';
        file_put_contents(SYSTEM_ROOT . 'config.php', $config);
    }
    require_once '../includes/common.php';
    $sqls = file_get_contents('update1.sql');
    $explode = explode(';', $sqls);
//$num = count($explode);
    $explode[] = "INSERT INTO `pre_sets` VALUES ('admin_pwd', '" . $conf['admin_pwd'] . "'), ('admin_user', '" . $conf['admin_user'] . "'), ('syskey', '" . $conf['syskey'] . "');";
    $version = 1001;
    unlink(ROOT . 'admin/edit.php');
    unlink(ROOT . 'admin/list.php');
    unlink(ROOT . 'admin/login.php');
    unlink(ROOT . 'admin/mail.php');
    foreach ($explode as $sql) {
        if ($sql = trim($sql)) {
            $DB->exec($sql);
        }
    }
    saveSetting('web_version', $version);
    exit("<script language='javascript'>alert('网站数据库v" . $version . "升级完成！');window.location.href='../';</script>");
} elseif ($conf['web_version'] < 1002) {
    $version = 1002;
    unlink(SYSTEM_ROOT . 'email.class.php');
    unlink(SYSTEM_ROOT . 'QC.class.php');
    unlink(SYSTEM_ROOT . 'PdoHelper.php');
    deldir(ROOT . 'static');
    @rmdir(ROOT . 'static');
    $sqls = file_get_contents('update2.sql');
} elseif ($conf['web_version'] < 1002) {
    $version = 1002;
    unlink(SYSTEM_ROOT . 'lib/email.class.php');
    $sqls = file_get_contents('update3.sql');
} elseif ($conf['web_version'] < 1003) {
    $version = 1003;
    deldir(ROOT . 'assets/css');
    @rmdir(ROOT . 'assets/css');
    unlink(ROOT . 'doc.php');
    $sqls = file_get_contents('update4.sql');
} elseif ($conf['web_version'] < 1004) {
    $version = 1004;
    @unlink(SYSTEM_ROOT . 'email.class.php');
    @unlink(SYSTEM_ROOT . 'QC.class.php');
    @unlink(SYSTEM_ROOT . 'PdoHelper.php');
    @deldir(ROOT . 'static');
    @rmdir(ROOT . 'static');
    @unlink(SYSTEM_ROOT . 'lib/email.class.php');
    @deldir(ROOT . 'assets/css');
    @rmdir(ROOT . 'assets/css');
    @unlink(ROOT . 'doc.php');
    @deldir(ROOT . 'admin/static');
    @rmdir(ROOT . 'admin/static');
    @unlink(ROOT . 'admin/account.php');
    @unlink(ROOT . 'admin/head.php');
    @unlink(ROOT . 'admin/log.php');
    @unlink(ROOT . 'admin/notice.php');
    @unlink(ROOT . 'admin/order.php');
    @unlink(ROOT . 'admin/set.php');
    @unlink(ROOT . 'admin/site.php');
    @unlink(ROOT . 'admin/update.php');
    $sqls = file_get_contents('update4.sql');
} else {
    unlink('update.lock');
    exit('你的网站已经升级到最新版本了，<a href="../">返回首页</a><br>返回首页后如果再次提示需要更新请清除浏览器缓存后重试<br>或联系作者QQ：<a href="' . kfqq(1604114199) . '">1604114199</a>');
}
$explode = explode(';', $sqls);
//$num = count($explode);
$error = 0;
$success = 0;
$errormsg = null;
foreach ($explode as $sql) {
    if ($sql = trim($sql)) {
        if ($DB->exec($sql) === false) {
            $error++;
            $errormsg .= '执行sql语句：' . $sql . '<br>返回错误:' . $DB->error() . '<br><br>';
        } else {
            $success++;
        }
    }
}
saveSetting('web_version', $version);
$CACHE->clear();
echo $error > 0 ? '网站数据库v' . $version . '升级完成！<br>成功执行：' . $success . '条语句<br>失败' . $error . '条语句<br>错误日志：' . $errormsg . '<br>请查看日志选择性的把错误的sql语句复制到数据库里重新执行<br><a href="../">更新完毕，返回首页</a>' : exit("<script language='javascript'>alert('网站数据库v" . $version . "升级完成！<br>成功执行：" . $success . "条语句');window.location.href='../';</script>");
